package com.novel.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.novel.base.entity.R;
import com.novel.web.entity.NfNovel;
import com.novel.web.service.NfNovelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 小说表
 *
 * @author 樊温军
 * @date Fri May 09 15:15:32 CST 2025
 */
@Slf4j
@Api(tags = "小说表", value = "小说表")
@RestController
@AllArgsConstructor
@RequestMapping("/nfNovel")
public class NfNovelController {

    private final NfNovelService nfNovelService;

    /**
     * 小说表—列表
     *
     * @param nfNovel 小说表
     * @return R<List < NfNovel>>
     */
    @ApiOperation(value = "小说表—列表", notes = "小说表—列表")
    @GetMapping("/list")
    public R<List<NfNovel>> getNfNovelList(NfNovel nfNovel) {
        log.info("开始查询小说表—列表，参数：{}", nfNovel);
        try {
            return R.ok(nfNovelService.getNfNovelList(nfNovel), "查询成功");
        } catch (Exception e) {
            log.error("查询小说表—列表时发生错误：{}", e.getMessage(), e);
            return R.failed("查询小说表—列表时发生错误，请稍后重试！");
        }
    }

    /**
     * 小说表—列表—分页
     *
     * @param page    分页对象
     * @param nfNovel 小说表
     * @return R<IPage < NfNovel>>
     */
    @ApiOperation(value = "小说表—列表—分页", notes = "小说表—列表—分页")
    @GetMapping(value = "/page")
    public R<IPage<NfNovel>> getNfNovelPage(Page<NfNovel> page, NfNovel nfNovel) {
        log.info("开始查询小说表—列表—分页，第 {} 页，每页 {} 条记录，其他参数：{}", page.getCurrent(), page.getSize(), nfNovel);
        try {
            IPage<NfNovel> iPage = nfNovelService.getPage(page, nfNovel);
            return R.ok(iPage, "查询成功");
        } catch (Exception e) {
            log.error("查询小说表—列表—分页时发生错误：{}", e.getMessage(), e);
            return R.failed("查询小说表—列表—分页时发生错误，请稍后重试！");
        }
    }

    /**
     * 小说表—保存
     *
     * @param nfNovel 小说表
     * @return R<Long>
     */
    @ApiOperation(value = "小说表—保存", notes = "小说表—保存")
    @PostMapping(value = "/saveOne")
    public R<Long> saveOneNfNovel(@Valid @RequestBody NfNovel nfNovel) {
        log.info("开始保存小说表：{}", nfNovel);
        try {
            Long id = nfNovelService.saveOneNfNovel(nfNovel);
            log.info("小说表保存成功，ID: {}", id);
            return R.ok(id, "保存成功");
        } catch (Exception e) {
            log.error("保存小说表时发生错误：{}", e.getMessage(), e);
            return R.failed("保存小说表时发生错误，请稍后重试！");
        }
    }

    /**
     * 小说表—删除
     *
     * @param id 小说表主键
     * @return R<Boolean>
     */
    @ApiOperation(value = "小说表—删除", notes = "小说表—删除")
    @PostMapping(value = "/delById")
    public R<Boolean> delByIdNfNovel(Long id) {
        log.info("开始删除小说表：{}", id);
        if (id == null || id <= 0) {
            log.warn("ID为空或无效: {}", id);
            return R.failed("请传入有效的ID！");
        }
        try {
            nfNovelService.delById(id);
            return R.ok(Boolean.TRUE, "删除成功");
        } catch (Exception e) {
            log.error("删除小说表失败: ID={}, Error={}", id, e.getMessage(), e);
            return R.failed("删除失败，请稍后重试！");
        }
    }

    /**
     * 小说表—详情
     *
     * @param id 小说表主键
     * @return R<NfNovel>
     */
    @ApiOperation(value = "小说表—详情", notes = "小说表—详情")
    @GetMapping(value = "/details")
    public R<NfNovel> getNfNovel(Long id) {
        log.info("开始查询小说表：{}", id);
        if (id == null || id <= 0) {
            log.warn("ID为空或无效: {}", id);
            return R.failed("请传入有效的ID！");
        }
        try {
            NfNovel nfNovel = nfNovelService.queryById(id);
            if (nfNovel != null) {
                return R.ok(nfNovel, "查询成功");
            } else {
                return R.failed("未找到小说表");
            }
        } catch (Exception e) {
            log.error("查询小说表详情时发生错误：{}", e.getMessage(), e);
            return R.failed("查询小说表详情时发生错误，请稍后重试！");
        }
    }
}
